﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using MINERP.InfoConfig.Controller;
using MINERP.DAL;

namespace MINERP.InfoConfig
{
    public partial class FrmWorkerGroup : FrmMDIChildFormTemplate
    {
        private WorkerGroupController _controller = new WorkerGroupController();
        tb_WorkerGroup _group = null;
        
        private static FrmWorkerGroup instance = null;
        private FrmWorkerGroup()
        {
            InitializeComponent();
        }

        public static FrmWorkerGroup GetInstance
        {
            get
            {
                if (instance == null)
                {
                    instance = new FrmWorkerGroup();
                }
                return instance;
            }
        }

        private void FrmWorkerGroup_Load(object sender, EventArgs e)
        {
            _controller.GridControl = gridControlWorkerGroup;
            _controller.View = gridViewWorkerGroup;
            _controller.GetDate();

        }

        private void gridViewWorkerGroup_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            _group = (tb_WorkerGroup)gridViewWorkerGroup.GetFocusedRow();
            if(_group!=null)
            {
                txtWorkerGroup.Text = _group.GroupName;
            }
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            if (btnAdd.Text != "保存")
            {
                PrepareAdd(true);
                txtWorkerGroup.Focus();
                return;
            }
            if (CheckData())
            {
                tb_WorkerGroup obj = new tb_WorkerGroup();

                obj.GroupName = txtWorkerGroup.Text;
                obj.Status = 1;
                _controller.Add(obj);

                //link datagrid
                _controller.GridControl = gridControlWorkerGroup;
                _controller.View = gridViewWorkerGroup;
                _controller.GetDate();

                PrepareAdd(false);
                MessageBox.Show("添加成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            if (btnUpdate.Text != "保存")
            {
                PrepareUpdate(true);
                txtWorkerGroup.Focus();
                return;
            }
            if (CheckData())
            {
                _group.GroupName = txtWorkerGroup.Text;
                _controller.Update();

                //link datagrid
                _controller.GridControl = gridControlWorkerGroup;
                _controller.View = gridViewWorkerGroup;
                _controller.GetDate();
                PrepareUpdate(false);

                MessageBox.Show("更新成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (DialogResult.OK == MessageBox.Show("确认删除吗?", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question))
            {

                _controller.Delete(_group);

                //link datagrid
                _controller.GridControl = gridControlWorkerGroup;
                _controller.View = gridViewWorkerGroup;
                _controller.GetDate();
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            txtWorkerGroup.Enabled = false;

            btnAdd.Text = "添加";
            btnUpdate.Text = "更新";
            btnUpdate.Enabled = true;
            btnAdd.Enabled = true;
            btnDelete.Enabled = true;
        }

        /// <summary>
        /// 检查数据完整性
        /// </summary>
        /// <returns></returns>
        private bool CheckData()
        {
            bool flag = true;
            if (txtWorkerGroup.Text == string.Empty)
            {
                txtWorkerGroup.Focus();
                MessageBox.Show("工作组名称不能为空", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
            return flag;
        }
        /// <summary>
        /// 添加前控件设置
        /// </summary>
        /// <param name="bl"></param>
        private void PrepareAdd(bool bl)
        {
            txtWorkerGroup.Enabled = bl;
             txtWorkerGroup.Text = string.Empty;


            if (bl)
            {

                btnAdd.Text = "保存";
                btnUpdate.Enabled = false;
                btnDelete.Enabled = false;
            }
            else
            {
                btnAdd.Text = "添加";
                btnUpdate.Enabled = true;
                btnDelete.Enabled = true;
            }
        }

        /// <summary>
        /// 更新前控件设置
        /// </summary>
        /// <param name="bl"></param>
        private void PrepareUpdate(bool bl)
        {

            txtWorkerGroup.Enabled = bl;

            if (bl)
            {

                btnUpdate.Text = "保存";
                btnAdd.Enabled = false;
                btnDelete.Enabled = false;
            }
            else
            {
                btnUpdate.Text = "更新";
                btnAdd.Enabled = true;
                btnDelete.Enabled = true;
            }
        }

        private void FrmWorkerGroup_FormClosed(object sender, FormClosedEventArgs e)
        {
            instance = null;
        }


    }
}
